﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmTracuuGV : Form
    {
        int i = 0;
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");
        public frmTracuuGV()
        {
            InitializeComponent();
            //Lookup Khoa
            string get_lkuMakhoa = "select Makhoa,Tenkhoa from T16_Khoa";
            DataSet ds1 = FillDataset(get_lkuMakhoa);
            lkuMaKhoa.Properties.DataSource = ds1.Tables[0];
            lkuMaKhoa.Properties.DisplayMember = "Makhoa";
            lkuMaKhoa.Properties.ValueMember = "Makhoa";
            //lookup trinh do
            string get_lkuMaTD = "select MatrinhdoID,TenTrinhDo from T06_Trinh_do";
            DataSet ds2 = FillDataset(get_lkuMaTD);
            lkuTrinhDo.Properties.DataSource = ds2.Tables[0];
            lkuTrinhDo.Properties.DisplayMember = "TenTrinhDo";
            lkuTrinhDo.Properties.ValueMember = "MatrinhdoID";
            //Loai GV
            string get_lkuLoaiGiangVien = "SELECT distinct LoaiGV FROM dbo.T02_Giao_Vien";
            DataSet ds3 = FillDataset(get_lkuLoaiGiangVien);
            lkuLoaiGV.Properties.DataSource = ds3.Tables[0];
            lkuLoaiGV.Properties.DisplayMember = "LoaiGV";
            lkuLoaiGV.Properties.ValueMember = "LoaiGV";

            //Chuc danh
            string get_lkuChucDanh = "SELECT DISTINCT HocHam FROM dbo.T02_Giao_vien";
            DataSet ds4 = FillDataset(get_lkuChucDanh);
            lkuChucDanh.Properties.DataSource = ds4.Tables[0];
            lkuChucDanh.Properties.DisplayMember = "HocHam";
            lkuChucDanh.Properties.ValueMember = "HocHam";
        }
        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            return ds;
        }
        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        DataSet dstimkiem = new DataSet();
        private void butTimKiem_Click(object sender, EventArgs e)
        {
            i = 1;
            string phai = "";
            string ttgd = "";
            DateTime d;
            if (dNgaysinh.Text != "")
            {
                d = dNgaysinh.DateTime;
                if (rgPhai.SelectedIndex == 0)
                    phai = "1";

                else if (rgPhai.SelectedIndex == 1)
                    phai = "0";
                if (raTTGD.SelectedIndex == 0)
                    ttgd = "0";
                else if (raTTGD.SelectedIndex == 1)
                    ttgd = "1";

                //string sql = "select * from View_TimKiemGiangVien where [Ngaysinh] = @NS";
                try
                {
                    string sql = "Select * from [dbo].[View_TimKiemGiangVien] where [MagvID] like N'%" + txtMaGV.Text + "%'"
                    + "and [Hoten] like N'%" + txtHoten.Text + "%'"
                    + "and [NgaySinh] = @NS "
                    + "and [Tentrinhdo] like N'%" + lkuTrinhDo.Text + "%'"
                    + "and [Phai] like N'%" + phai + "%'"
                    + "and [Noisinh] like N'%" + txtNoiSinh.Text + "%'"
                    + "and [HocHam] like N'%" + lkuChucDanh.Text + "%'"
                    + "and [LoaiGV] like N'%" + lkuLoaiGV.Text + "%'"
                    + "and [MaKhoa] like N'%" + lkuMaKhoa.Text + "%'"
                    + "and [TinhtrangGD] like N'%" + ttgd + "%'";

                    SqlCommand com = new SqlCommand(sql, connection);
                    com.Parameters.Add(new SqlParameter("@NS", d));
                    SqlDataAdapter da = new SqlDataAdapter(com);
                    
                    da.Fill(dstimkiem, "GV");
                    gridControl1.DataSource = dstimkiem.Tables["GV"];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            else if (dNgaysinh.Text == "")
            {
                //d = dNgaysinh.DateTime;
                if (rgPhai.SelectedIndex == 0)
                    phai = "1";
                else if (rgPhai.SelectedIndex == 1)
                    phai = "0";
                if (raTTGD.SelectedIndex == 0)
                    ttgd = "0";
                else if (raTTGD.SelectedIndex == 1)
                    ttgd = "1";

                try
                {
                    string sql = "Select * from [dbo].[View_TimKiemGiangVien] where [MagvID] like N'%" + txtMaGV.Text + "%'"
                    + "and [Hoten] like N'%" + txtHoten.Text + "%'"
                    + "and [Tentrinhdo] like N'%" + lkuTrinhDo.Text + "%'"
                    + "and [Phai] like N'%" + phai + "%'"
                    + "and [Noisinh] like N'%" + txtNoiSinh.Text + "%'"
                    + "and [HocHam] like N'%" + lkuChucDanh.Text + "%'"
                    + "and [LoaiGV] like N'%" + lkuLoaiGV.Text + "%'"
                    + "and [MaKhoa] like N'%" + lkuMaKhoa.Text + "%'"
                    + "and [TinhtrangGD] like N'%" + ttgd + "%'";

                    dstimkiem = FillDataset(sql);
                    gridControl1.DataSource = dstimkiem.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        DataSet ds_load = new DataSet();
        private void frmTracuuGV_Load(object sender, EventArgs e)
        {
            i = 0;
            string sql = "Select * from [dbo].[View_TimKiemGiangVien]";
             ds_load = FillDataset(sql);
            gridControl1.DataSource = ds_load.Tables[0];
        }

        private void butXoaTatCa_Click(object sender, EventArgs e)
        {
            txtMaGV.Text = "";
            txtHoten.Text = "";
            txtNoiSinh.Text = "";
            dNgaysinh.Text = "";
            rgPhai.SelectedIndex = -1;
            lkuChucDanh.EditValue = "";
            lkuLoaiGV.EditValue = "";
            lkuMaKhoa.EditValue = "";
            lkuTrinhDo.EditValue = "";
            raTTGD.SelectedIndex = -1;



        }

        string magv;
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            magv = gridView1.GetRowCellValue(e.RowHandle, colMagvID).ToString();
            string sql = "select * from T10_Qua_trinh_dao_tao where MagvID ='" + magv + "'";
            DataSet ds = FillDataset(sql);
            gridControl2.DataSource = ds.Tables[0];
        }

        private void btnXemthongtinGV_Click(object sender, EventArgs e)
        {
            rptThongtinveGV rpt = new rptThongtinveGV();
            DataSet ds = FillDataset("select * from ThongtinveGV where MagvID = '" + magv + "'");
            //if(ds.Tables[0].Rows.Count == 0)

            DataRow row = ds.Tables[0].Rows[0];
            string status = row["TinhtrangGD"].ToString();
            if (status == "False")
                status = "Độc thân";
            else
                status = "Kết hôn";
            rpt.DataSource = ds.Tables[0];
            rpt.BindData(status);
            rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
            rpt.ShowPreview();

        }

        private void btnInDStimthay_Click(object sender, EventArgs e)
        {
            rptDanhsachGVtruong rpt = new rptDanhsachGVtruong();
            if (i == 0)
                rpt.DataSource = ds_load.Tables[0];
            else
                rpt.DataSource = dstimkiem.Tables["GV"];
            rpt.BindData();
            rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
            rpt.ShowPreview();
        }

        private void btnInthongtinGiangday_Click(object sender, EventArgs e)
        {

            rptThongtinGiangdayGV rpt = new rptThongtinGiangdayGV();
            rpt.DataSource = FillDataset("select * from T10_Qua_trinh_dao_tao where MagvID ='" + magv + "'");
            rpt.BindData();
            rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
            rpt.ShowPreview();
        }
    }
}
